package com.jt.controller;

import com.jt.pojo.User;
import com.jt.sevrice.UserSevrice;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class userController {
    @Autowired
    private UserSevrice userSevrice;
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
            String token =userSevrice.login(user);
        if (StringUtils.hasLength(token)){
            return SysResult.success(token);
        }return SysResult.fail();
    }
    @GetMapping("/list")
    public  SysResult getUserList(PageResult pageResult){
        pageResult=userSevrice.getUserList(pageResult);
        return SysResult.success(pageResult);
    }
    @PutMapping("/status/{id}/{status}")
    public SysResult updateById(@PathVariable User user){
        userSevrice.updateById(user);
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userSevrice.addUser(user);
        return SysResult.success();
    }
    @DeleteMapping("/{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        try {
            userSevrice.deleteById(id);
            return SysResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.fail();
        }
    }
}
